import { requestData } from "@/types/type";
import axios from "axios";
// import config from "../config";
import config from "../config";
// 请求超时时间
axios.defaults.timeout = 100000;
// 请求baseurl
axios.defaults.baseURL = config.baseUrl || config.mockUrl;
// 添加请求拦截器
axios.interceptors.request.use(
  function (config: any) {
    // 在发送请求之前做些什么
    console.log("发送请求");
    return config;
  },
  function (error) {
    console.log("错误");
    // 对请求错误做些什么
    errorCode(error);
    return Promise.reject(error);
  }
);
// 添加响应拦截器
axios.interceptors.response.use(
  function (response: any) {
    // 2xx 范围内的状态码都会触发该函数。
    // 对响应数据做点什么
    console.log("响应拦截");
    return response;
  },
  function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    console.log(error);
    // 对响应错误做点什么
    errorCode(error);
    return Promise.reject(error);
  }
);
// 错误状态码返回
const errorCode = (err: any): any => {
  if (err && err.response) {
    switch (err.response.status) {
      case 400:
        console.log(err.response.data.error.details);
        break;
      case 401:
        console.log("未授权,请登录");
        break;
      case 403:
        console.log("拒绝访问");
        break;
      case 404:
        console.log("请求地址出错");
        break;
      case 408:
        console.log("请求超时");
        break;
      case 500:
        console.log("服务器内部错误");
        break;
      case 501:
        alert("服务未实现");
        break;
      case 502:
        console.log("网关错误");
        break;
      case 503:
        console.log("服务不可用");
        break;
      case 504:
        console.log("网关超时");
        break;
      case 505:
        console.log("HTTP版本不受支持");
        break;
      default:
        break;
    }
  }
};
// 封装get请求
export function get(url: string, params = {}) {
  return new Promise(async (resolve, reject) => {
    try {
      const response = await axios.get(url, { params: params });
      resolve(response.data);
    } catch (error) {
      reject(error);
    }
  });
}
// 封装put请求
export function put(url: string, data = {}) {
  return new Promise(async (resolve, reject) => {
    try {
      const response = await axios.put(url, { data: data });
      console.log(response);
      resolve(response.data);
    } catch (error) {
      reject(error);
    }
  });
}
//删除数据
export function del(url: string, params = {}) {
  return new Promise(async (resolve, reject) => {
    try {
      const response = await axios.delete(url, { params: params });
      resolve(response.data);
    } catch (error) {
      reject(error);
    }
  });
}
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(
  url: string,
  data: string | object | ArrayBuffer
): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const response = await axios.post(url, data);
      resolve(response.data);
    } catch (error) {
      reject(error);
    }
  });
}

// interface httpData{
//   url: string;
//   method: string;
//   data: any;
//   mock: boolean;
// }
function http(Data: requestData) {
  let { url, method, data, mock } = Data;
  // 判断是否使用mock数据
  let isMock = mock && config.mock;
  // 生产模式不允许mock形式
  if (config.ENV == "pro") {
    axios.defaults.baseURL = config.baseUrl;
  } else {
    axios.defaults.baseURL = isMock ? config.mockUrl : config.baseUrl;
  }
  return new Promise((resolve, reject) => {
    switch (method) {
      case "GET":
        get(url, data)
          .then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
        break;
      case "PUT":
        put(url, data)
          .then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
        break;
      case "DEL":
        del(url, data)
          .then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      case "POST":
        post(url, data)
          .then((response) => {
            resolve(response);
          })
          .catch((error) => {
            reject(error);
          });
      default:
        break;
    }
  });
}

export default http;
